/*************************************************************************
 * Filename:    Testing.c
 * Description: Runs the test interface. Accepts user input and sends it 
 *              to Commands.c
 * Group 2:     nachOS
 * Authors:     Daniel Fairweather
 * Class:       CSE 325
 * Instructor:  Mr. Duggan
 * Assignment:  Lab #3 Scheduler
 * Assigned:    Feburary 12, 2013
 * Due:         Feburary 26, 2013
 *************************************************************************/
#include "Testing.h"

char buffer[1000];      /* Buffer for user input. */
char command[1000];     /* Stores the previous command. */
int testMode = 1;       /* The test mode the user is running. */



void clearScreen(){
    system("clear");
}



int getTestMode(){
    return(testMode);
}



int setTestMode(int i){
    clearScreen();
    testMode = i;
}



void printdiv(){
    printf("-------------------------------------------------------------------------\n");
}



void printSystemInformation(){
    printdiv();
    printf("OS Name:\tnachOS\n");
    printf("Group #:\t2\n");
    printf("Scheduler:\t");
    
    switch(scheduler_algorithm){
        case 0:
            printf("Fair-Share\n");
            printf("PCB Format:\t{PID, GID}");
            break;
        case 1:
            printf("Priority with Aging\n");
            printf("PCB Format:\t{PID, GID, AGE}");
            break;
    }
    printf("\n");
    printdiv();
}



void printProcess(process_control_block* pcb){
    if(scheduler_algorithm == 0){
        printf("{%i, %i}", pcb->pid, pcb->gid);
    }
    else if(scheduler_algorithm == 1){
        printf("{%i, %i, %i}", pcb->pid, pcb->gid, pcb->age);
    }
}



void listQueue(int qid){
    process_control_block* pcb = queues[qid].head;
    int smallest_pid = 999999999;
    int last_smallest_pid = -1;
    int last_pid = 0;
    int first_pcb = 1;

    if(pcb == NULL){
        printf("Empty\n");
        return;
    }
    while(1){
        if(pcb == NULL){
            if(last_pid == 1){
                printf("\n");
                break;
            }
            else if(first_pcb == 0){
                printf(", ");
            }
            first_pcb = 0;
            printProcess(search(qid, smallest_pid));
            
            last_smallest_pid = smallest_pid;
            smallest_pid = 999999999;
            pcb = queues[qid].head;
            last_pid = 1;
            continue;
        }
        if(pcb->pid > last_smallest_pid && pcb->pid < smallest_pid){
            smallest_pid = pcb->pid;
            last_pid = 0;
        }
        pcb = pcb->next;
    }
    
}



void printQueue(int qid){
    process_control_block* pcb = queues[qid].head;
    if(pcb == NULL){
        printf("Empty\n");
    }
    while(pcb != NULL){
        if(pcb == queues[qid].tail){
            printProcess(pcb);
            printf("\n");
        }
        else{
            printProcess(pcb);
            printf(", ");
        }
        pcb = pcb->next;
    }
}



void printQueues(){
    printf("New:\t\t");
    printQueue(0);
    printf("Ready:\t\t");
    printQueue(1);
    printf("Running:\t");
    printQueue(2);
    printf("Waiting:\t");
    printQueue(3);
    printf("Terminated:\t");
    printQueue(4);
}



void listQueues(){
    printf("New:\t\t");
    listQueue(0);
    printf("Ready:\t\t");
    listQueue(1);
    printf("Running:\t");
    listQueue(2);
    printf("Waiting:\t");
    listQueue(3);
    printf("Terminated:\t");
    listQueue(4);
}


int main(){
    int i = 1;
    init(0);
    while(1){
        if(testMode == 2){
            clearScreen();
            printSystemInformation();
            printQueues();
            printdiv();
            printf("Previous Command: %s\n", buffer);
            printf("Result:           %s\n", result);
            printdiv();
        }
        printf(">>> ");
        if(fgets(buffer, 1000, stdin) != NULL){
            for(i = 0; buffer[i]; i++){
                buffer[i] = tolower(buffer[i]);
            }
            buffer[strlen(buffer)-1] = '\0';
            strcpy(command, buffer);
            if(strlen(command) >= 1 && command[0] != '*'){
                if(testMode == 0){
                    printf("%s\n", command);
                }
                processCommand(command);
            }
			else if(testMode == 0){
                printf("\n\n\n%s\n", command);
            }
        }
        else{
            return(1);
        }
    }

    return(1);
}